//
//  MCircleListCell.m
//  HAODE
//
//  Created by panquanReabam.com on 15/10/19.
//  Copyright (c) 2015年 reabam.com. All rights reserved.
//



#import "MCircleListCell.h"
#import "UIImageView+WebCache.h"
#import "UIButton+WebCache.h"
#import "HttpRequest.h"
#import "UIImageView+MJWebCache.h"
#import "MJPhotoBrowser.h"
#import "MJPhoto.h"
#define  lineColor  [UIColor colorWithRed:228.00/255.0 green:228.00/255.0 blue:228.00/255.0 alpha:1]

#import "UIViewExt.h"
#define ScreenHeight [UIScreen mainScreen].bounds.size.height
#define ScreenWidth  [UIScreen mainScreen].bounds.size.width

@interface MCircleListCell ()<UICollectionViewDataSource,UICollectionViewDelegate>
{
    NSArray *array;
    NSInteger collectionHeight;
    UIView *containerView;
    UICollectionView *collection;
    UIImageView* images;
    NSArray *_urls;
  

}
@end
@implementation MCircleListCell{
    
    
}

- (void)awakeFromNib {
    _urls = @[@"http://ww4.sinaimg.cn/thumbnail/7f8c1087gw1e9g06pc68ug20ag05y4qq.gif", @"http://ww3.sinaimg.cn/thumbnail/8e88b0c1gw1e9lpr0nly5j20pf0gygo6.jpg", @"http://ww4.sinaimg.cn/thumbnail/8e88b0c1gw1e9lpr1d0vyj20pf0gytcj.jpg", @"http://ww3.sinaimg.cn/thumbnail/8e88b0c1gw1e9lpr1xydcj20gy0o9q6s.jpg", @"http://ww2.sinaimg.cn/thumbnail/8e88b0c1gw1e9lpr2n1jjj20gy0o9tcc.jpg", @"http://ww2.sinaimg.cn/thumbnail/8e88b0c1gw1e9lpr39ht9j20gy0o6q74.jpg", @"http://ww3.sinaimg.cn/thumbnail/8e88b0c1gw1e9lpr3xvtlj20gy0obadv.jpg", @"http://ww4.sinaimg.cn/thumbnail/8e88b0c1gw1e9lpr4nndfj20gy0o9q6i.jpg", @"http://ww3.sinaimg.cn/thumbnail/8e88b0c1gw1e9lpr57tn9j20gy0obn0f.jpg"];

}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _imageArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str = @"collection";
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:str forIndexPath:indexPath];
    for (UIView *view in [cell.contentView subviews]) {
        [view removeFromSuperview];
    }
    collectionView .userInteractionEnabled = YES;
    images=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, cell.width, cell.height)];
    [images setImageWithURL:[NSURL URLWithString:_imageArray[indexPath.row]]  placeholderImage:[UIImage imageNamed:@"logo_normalss.png"]];
    images.tag = indexPath.row;
    
    images.userInteractionEnabled = YES;
    [images addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImage:)]];
    
    //images 内容模式
    images.clipsToBounds = YES;
    images.contentMode = UIViewContentModeScaleAspectFill;
  
    [cell addSubview:images];
    
    return cell;
}



- (void)tapImage:(UITapGestureRecognizer *)tap
{
   int count = _imageArray.count;
    // 1.封装图片数据
    NSMutableArray *photos = [NSMutableArray arrayWithCapacity:count];
    for (int i = 0; i<count; i++) {
        // 替换为中等尺寸图片
        NSString *url = [_imageArray[i] stringByReplacingOccurrencesOfString:@"thumbnail" withString:@"bmiddle"];
        MJPhoto *photo = [[MJPhoto alloc] init];
        photo.url = [NSURL URLWithString:url]; // 图片路径
        
//        photo.srcImageView = image.subviews[i]; // 来源于哪个UIImageView
        [photos addObject:photo];
    }
    
    // 2.显示相册
    MJPhotoBrowser *browser = [[MJPhotoBrowser alloc] init];
    browser.currentPhotoIndex = tap.view.tag; // 弹出相册时显示的第一张图片是？
    browser.photos = photos; // 设置所有的图片
    [browser show];
}

#define kYOfImageContainer   136.0

- (void)setComment:(MCirclemodel *)comment
{
    _comment = comment;
    _imageArray = comment.imagePicBig;
    
    
    _image.layer.masksToBounds = YES;
    _image.layer.cornerRadius = 40/2;
    _image.layer.borderColor = [UIColor whiteColor].CGColor;
    _image.layer.borderWidth = 0;
    _image.contentMode = UIViewContentModeScaleAspectFill;
    _image.userInteractionEnabled = YES;
    [_image setImageWithURL:[NSURL URLWithString:comment.headImageUrl]  placeholderImage:[UIImage imageNamed:@"damorentouxiang"]];
    
    
    
    if ([comment.isPraise isEqualToString:@"Y"] ) {
        
        self.guangzhuimage.image=[UIImage imageNamed:@"dainzan-hight"];
       
        
    }else{
        
        self.guangzhuimage.image=[UIImage imageNamed:@"dainzan"];
        
        
        
        
    }

    self.diselabelview.layer.borderWidth = 0.35;
    self.diselabelview.layer.borderColor = qianhuicolor.CGColor;
    self.nameLabel.text = comment.userName;
    self.dateLabel.text = comment.createdAt;
    self.commentCount.text = comment.commentCount;
    self.praiseCount.text = comment.praiseCount;
    self.guanzhu.titleLabel.text=comment.commentId;
   
    
    
    NSDictionary *attrbute = @{NSFontAttributeName:[UIFont systemFontOfSize:15]};
    CGFloat textHeight;
    if ([comment.mcontent isEqualToString:@""]) {
        textHeight = 35;
        self.conten.text =[NSString stringWithFormat:@"%@" ,@"暂无说明哦！"];
       
    }else{
        textHeight = [[NSString stringWithFormat:@"%@" ,comment.mcontent]
                      boundingRectWithSize:CGSizeMake(ScreenWidth-40, 1000) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attrbute context:nil].size.height +15;
         self.conten.text =[NSString stringWithFormat:@"%@" ,comment.mcontent];
    }
    
    
    
    [collection removeFromSuperview];
//    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
//    layout.itemSize = CGSizeMake((ScreenWidth-45)/4 , (ScreenWidth-45)/4);
//    layout.minimumInteritemSpacing = 1;
//    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
//    
//   
//   //    NSLog(@"%@",comment. imagePicSmall.count%4);
//    
//    
//    collectionHeight = (comment. imagePicSmall.count%4 +1) *(layout.itemSize.width + 10) -10;
//    
//    collection = [[UICollectionView alloc]initWithFrame:CGRectMake(12.5, textHeight+60+15, ScreenWidth-25, collectionHeight) collectionViewLayout:layout];
//    collection.backgroundColor = [UIColor grayColor];
//   collection.alwaysBounceVertical = NO;
//    [collection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"collection"];
//    collection.delegate = self;
//    collection.dataSource = self;
//   collection.userInteractionEnabled=NO;
   
    
//    collectionHeight = (comment. imagePicSmall.count%4 +1) *(layout.itemSize.width + 10) -10;
      if (comment.imagePicSmall>0) {
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    //设置单元格的尺寸
    flowLayout.itemSize = CGSizeMake(collectionCellHeight, collectionCellHeight);
    
    flowLayout.minimumInteritemSpacing = 5;
    flowLayout.minimumLineSpacing = 5;
    flowLayout.sectionInset = UIEdgeInsetsMake(15, 15, 15, 15);
    
   collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0,textHeight+60+10, ScreenWidth, collectionCellHeight+15*2) collectionViewLayout:flowLayout];
   collection.backgroundColor = [UIColor clearColor];
    collection.dataSource=self;
   collection.delegate=self;
    
    [collection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"collection"];
  
   
    int numberOfLines = (int)ceil([self collectionView:collection numberOfItemsInSection:0] / 4.0);
    
    CGRect frame = collection.frame;
    frame.size.height = numberOfLines *collectionCellHeight + (numberOfLines - 1) * 4 + 15*2;
    collection.frame = frame;

    [self.contentView addSubview:collection];
      }else{
      }

}

+ (CGFloat)cellHeight:(MCirclemodel *)commentModel
{
    // 手动计算一下containerView的高度，拿此高度再加其它控件的高度
    NSDictionary *attrbute = @{NSFontAttributeName:[UIFont systemFontOfSize:15]};
    CGFloat textHeight;
    if ([commentModel.mcontent isEqualToString:@""]) {
        textHeight = 5;
    }else{
        textHeight = [[NSString stringWithFormat:@"%@" ,commentModel.mcontent]
                      boundingRectWithSize:CGSizeMake(ScreenWidth-40, 1000) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attrbute context:nil].size.height +15;
    }
    
    
//    CGFloat headImageHeight = 110;
//    
//    //collectionView height
//    NSInteger arrayCount = commentModel.imagePicSmall.count;
//    
//    CGFloat collectionH = (arrayCount/4 +1) *((ScreenWidth-50)/4 + 10) -10;
//    
//    return  headImageHeight + textHeight +35 + collectionH;
    
    
    CGFloat headImageHeight = 140;
    
    
    
    if (commentModel.imagePicSmall.count==0) {
        CGFloat collectionH =0;
        return  headImageHeight + textHeight + collectionH;
    }else{
        NSInteger arrayCount = commentModel.imagePicSmall.count;
        int i = 1;
        if (arrayCount % 5 == 0) {
            i++;
        }
        CGFloat collectionH = (i) *((ScreenWidth-50)/4 + 10) -10;
        return  headImageHeight + textHeight +15 + collectionH;
    }

}









- (IBAction)pinglun:(id)sender {




    
    
}

- (IBAction)guanzhu:(id)sender {
    
   
    NSUserDefaults *user1 = [NSUserDefaults standardUserDefaults];
    NSString* tokenId = [user1 objectForKey:@"tokenId"];
    NSMutableDictionary *params = [NSMutableDictionary new];
    [params setValue:tokenId forKey:@"tokenId"];
    [params setValue:self.guanzhu.titleLabel.text forKey:@"mcId"];
     NSLog(@"-----%@",params);
  
    [HttpRequest requestData:@"/Discover/MCircle/Praise" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
        if (isSuccess) {
            
           
            
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {

                if ([[severDataDict.dataDict objectForKey:@"isPraise"]isEqualToString:@"Y"] ) {
                    
                    self.guangzhuimage.image=[UIImage imageNamed:@"dainzan-hight"];
                    double d;
                    d =self.praiseCount.text. doubleValue;
                    d++;
                    self.praiseCount.text=[NSString stringWithFormat:@"%.f",d];
                    
                }else{
                    
                   self.guangzhuimage.image=[UIImage imageNamed:@"dainzan"];
                    double d;
                    d =self.praiseCount.text. doubleValue;
                    d--;
                    self.praiseCount.text=[NSString stringWithFormat:@"%.f",d];
               
                
                
                }

                
                
            }else{
                
                
                
            }
            
        }
        else
        {
           
        }
        
    }];

    
}









@end
